package edu.ccut.saturn.studio.core.util.wizard;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;

import edu.ccut.saturn.studio.core.saturnproperty.StringUtils;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnEclipseSelectionUtil;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;

public abstract class SaturnWizard extends Wizard implements INewWizard {

	private IStructuredSelection selection;
	private Map<String, Object> values = new HashMap<String, Object>();
	private IWorkbench workbench;
	private List<SaturnWizardPage> pages = new ArrayList<SaturnWizardPage>();

	/**
	 * 返回生成文件名的抽像方法
	 * 
	 * @return
	 */
	protected abstract String getFileName();

	/**
	 * 获得FREEMAKER模板路径的抽像方法
	 * 
	 * @return
	 */
	protected abstract String getTemplatePath();

	protected abstract String getEditorID();

	@Override
	public void addPage(IWizardPage page) {
		super.addPage(page);

		if (page instanceof SaturnWizardPage) {
			SaturnWizardPage saturnWizardPage = (SaturnWizardPage) page;
			saturnWizardPage.setSelection(this.selection);
			this.pages.add(saturnWizardPage);
		}
	}

	public void initalValues() {
		if (this.pages != null) {
			for (SaturnWizardPage saturnWizardPage : this.pages) {
				saturnWizardPage.initalValues(this.values);
			}
		}
	}

	public boolean validate() {
		if (this.pages != null) {
			for (SaturnWizardPage saturnWizardPage : this.pages) {
				if (!saturnWizardPage.validate()) {
					return false;
				}
			}
		}

		return true;
	}

	public void generateFile() {
		String file = this.getFileFullPath();

		String templatePath = this.getTemplatePath();

		if (StringUtils.isNotEmpty(templatePath)) {
			FreemarkerUtils.writeTemplateToFile(this.getTemplatePath(),
					this.values, file);
		} else {
			File dirctory = new File(file);
			dirctory.mkdir();
		}

		SaturnProjectUtil.refreshProject(SaturnEclipseSelectionUtil
				.getProjectName(selection));

		openEditor(getFileProjectPath());
	}

	@Override
	public boolean performFinish() {
		initalValues();

		if (this.validate()) {
			generateFile();

			return true;
		}

		return false;
	}

	private void openEditor(String filePath) {
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		IFile file = (IFile) root.findMember(filePath);

		if (file != null && getEditorID() != null) {
			IDE.setDefaultEditor(file, getEditorID()); //$NON-NLS-1$
			try {
				IDE.openEditor(workbench.getActiveWorkbenchWindow()
						.getActivePage(), file, true);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
		this.workbench = workbench;
	}

	public IStructuredSelection getSelection() {
		return selection;
	}

	public void setSelection(IStructuredSelection selection) {
		this.selection = selection;
	}

	public void setWorkbench(IWorkbench workbench) {
		this.workbench = workbench;
	}

	public Map<String, Object> getValues() {
		return values;
	}

	public String getTargetPath() {
		return SaturnEclipseSelectionUtil.getFullPath(selection);
	}

	public String getFileProjectPath() {
		return SaturnEclipseSelectionUtil.getProjectPath(selection)
				+ File.separator + this.getFileName();
	}

	public String getFileFullPath() {
		return this.getTargetPath() + File.separator + this.getFileName();
	}
}
